package 尚硅谷juc大厂.test1;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@RestController
@Slf4j
public class FutureTaskDemo {

    @Autowired
    CallableServiceImpl callableService;

    /**
     * 为什么使用FutureTask：因为FutureTask能够操作当前线程【如：取消执行，获取线程返回的结果】
     */
    @GetMapping(value = "/future")
    public Integer future() throws ExecutionException, InterruptedException, TimeoutException {
        FutureTask<Integer> futureTask = new FutureTask<>(callableService);
        new Thread(futureTask,"AA").start();
        log.info("主线程继续执行.........................................");
        Integer result;
        while (true){
            if (futureTask.isDone()){
                result=futureTask.get();
                break;
            }
        }
        return result;
    }

}
